﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using TheOutfield.UmbExt.UniversalMediaPicker.Extensions;
using TheOutfield.UmbExt.UniversalMediaPicker.Controls;

namespace TheOutfield.UmbExt.UniversalMediaPicker.Providers.Picasa
{
    public class PicasaConfigControl : AbstractConfigControl
    {
        private PicasaProvider provider;
        private PicasaConfig config;
        private TextBox usernameTextBox = new TextBox();
        private TextBox passwordTextBox = new TextBox();

        public override string Value
        {
            get
            {
                EnsureChildControls();

                config.Username = usernameTextBox.Text;
                config.Password = passwordTextBox.Text;

                return config.SerializeToJson();
            }
        }

        public PicasaConfigControl(PicasaProvider provider, string options)
        {
            this.provider = provider;
            this.config = options.DeserializeJsonTo<PicasaConfig>();
        }

        protected override void OnLoad(EventArgs e)
        {
            if(!Page.IsPostBack)
            {
                usernameTextBox.Text = config.Username;
                passwordTextBox.Text = config.Password;
            }

            base.OnLoad(e);
        }

        protected override void CreateChildControls()
        {
            usernameTextBox.ID = "usernameTextBox";
            passwordTextBox.ID = "passwordTextBox";

            usernameTextBox.CssClass = "guiInputText guiInputStandardSize";
            passwordTextBox.CssClass = "guiInputText guiInputStandardSize";
            
            passwordTextBox.TextMode = TextBoxMode.Password;

            Controls.Add(usernameTextBox);
            Controls.Add(passwordTextBox);
        }

        protected override void Render(HtmlTextWriter writer)
        {
            writer.AddFormRow("Username:", "Please enter a valid Picasa username", usernameTextBox);
            writer.AddFormRow("Password:", "Please enter your Picasa password", passwordTextBox);
        }
    }
}